package com.lankystar.logistucs.spring.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;

import com.lankystar.logistucs.spring.entity.User;
import com.lankystar.logistucs.spring.entity.UserType;
import com.lankystar.logistucs.spring.service.UserService;
import com.syx.spring.utils.FileUtil;

@Controller
@RequestMapping(value="/user")
public class UserController {
/**
 * 用户
 */
	Map<String, Object> map = new HashMap<>();
	@Autowired
	private UserService userService;
	
	@RequestMapping(value="/toLogin.do")
	public String toLogin(){
		return "/user/login";
	}
	
	@RequestMapping(value="/toUpdate.do")
	public String toUpdate(Model model,Integer userId){
		User user = userService.selectUserById(userId);
		model.addAttribute("user", user);
		return "/user/update";
	}
	
	@RequestMapping(value="/login.do")
	public String login(Model model,User record,HttpSession ses){
		try {
			User user = userService.login(record.getUserPhone(), record.getUserPassword());
			ses.setAttribute("userSession", user);
			UserType userType= user.getUserType();
			/**
			 * 运输公司
			 */
			if(userType.getUserKey()==101||userType.getUserKey()==1010001){
				return "redirect:/company/toTrucklist.do";
			}
			/**
			 * 上海报关行
			 */
			if(userType.getUserKey()==102||userType.getUserKey()==1020001){
				return "redirect:/broker/queryOrderlist.do";
			}
			/**
			 * 苏州报关行
			 */
			if(userType.getUserKey()==103||userType.getUserKey()==1030001){
				return "redirect:/cargo/queryCargoInfolist.do";
			}
			Map<String, Object> data = new HashMap<>();
			data.put("userId", user.getUserId());
			data.put("userPhone", user.getUserPhone());
			data.put("userName", user.getUserName());
			map.put("data", data);
			map.put("msg", "用户登录成功！！");
			map.put("code", 1);
		} catch (Exception e) {
			map.put("msg", "用户登录失败,用户不存在！！");
			map.put("code", 0);
		}
		return "redirect:/user/toLogin.do";
	}
	
	@RequestMapping(value="/addUser.do",method=RequestMethod.POST)
	public String addUser(Model model,User record,MultipartFile file,HttpSession ses,HttpServletRequest req){
		try {
			if(file!=null){
				/**
				 * 上传驾驶证
				 */
				String drivLicence = FileUtil.uploadMethod(file, req);
				record.setDrivLicence(drivLicence);
			}
			userService.addUser(record);
			map.put("msg", "用户添加成功！！");
			map.put("code", 1);
		} catch (Exception e) {
			map.put("msg", "用户添加失败,参数不能为空！！");
			map.put("code", 0);
		}
		return "redirect:/user/queryUserlist.do";
	}
	
	@RequestMapping(value="/updateUser.do",method=RequestMethod.POST)
	public String updateUser(Model model,User record,HttpServletResponse resp){
		try {
			userService.updateUser(record);
			map.put("msg", "用户修改成功！！");
			map.put("code", 1);
		} catch (Exception e) {
			map.put("msg", "用户修改失败,参数异常！！");
			map.put("code", 0);
		}
		return "redirect:/user/queryUserlist.do";
	}
	
	@RequestMapping(value="/queryUserlist.do")
	public String queryUserlist(Model model,HttpSession ses){
		try {
			User user = (User) ses.getAttribute("userSession");
			List<User> userlist = userService.queryUserlist(user.getUserType());
			map.put("msg", "用户列表查询成功！！");
			map.put("code", 1);
			model.addAttribute("userlist", userlist);
		} catch (Exception e) {
			map.put("msg", "用户列表查询失败！！");
			map.put("code", 0);
		}
		return "/user/userlist";
	}
	
	@RequestMapping(value="/deleteUser.do")
	public String deleteUser(Integer ismove,Integer isusual,Integer userId){
		try {
			userService.deleteUser(ismove, isusual, userId);
			map.put("msg", "用户修改成功！！");
			map.put("code", 1);
		} catch (Exception e) {
			map.put("msg", "用户修改失败,参数异常！！");
			map.put("code", 0);
		}
		return "redirect:/user/queryUserlist.do";
	}
}
